package com.gooda.spring_boot_exp3.controller;

import com.gooda.spring_boot_exp3.common.Result;
import com.gooda.spring_boot_exp3.entity.VirusStats;
import com.gooda.spring_boot_exp3.service.VirusService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class VirusController {

    @Resource
    VirusService virusService;

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/list")
    public Result list() {
        return Result.success(virusService.getVirusData());
    }

    @GetMapping("/list/{country}")
    public Result searchByCountry(@PathVariable("country") String country) {
        List<VirusStats> data = virusService.getVirusData()
                .stream().filter(v -> v.getCountry().contains(country)).collect(Collectors.toList());
        if (data.size() == 0) {
            return Result.fail("不存在该国家的疫情信息");
        } else {
            return Result.success(200, "获取到国名包含：\""+country+"\" 的国家的疫情数据", data);
        }
    }


}
